package com.z.sys.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.z.sys.entity.User;
import com.z.sys.vo.UserInfoVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 员工管理Mapper接口
 *
 * @author z
 * @date 2022-11-07
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    /**
     * 查询员工管理列表
     *
     * @param user 员工管理
     * @return 员工管理集合
     */
    List<User> selectUserList(User user);

    /**
     * 更新enable状态
     * @param user
     */
    boolean updateEnableById(User user);



    /**
     * 根据人员id查询基本信息
     * @param userId
     * @return
     */
    @Select("SELECT u.head_img_url,u.id,u.username,u.user_sn, d.* " +
            "FROM t_user u LEFT JOIN (select distinct `d`.`id` AS `dept_id`,`d`.`code` AS `dept_code`,`d`.`name_abbr` AS `dept_name`,(select substring_index(`dt`.`code`,'-',1) from `t_dept` `dt` where (`dt`.`id` = `d`.`id`)) AS `unit_code`,(select substring_index(`dt`.`code`,'-',if(((length(`dt`.`code`) - length(replace(`dt`.`code`,'-',''))) > 0),2,0)) from `t_dept` `dt` where (`dt`.`id` = `d`.`id`)) AS `department_code`,(select substring_index(`dt`.`code`,'-',if(((length(`dt`.`code`) - length(replace(`dt`.`code`,'-',''))) = 2),3,0)) from `t_dept` `dt` where (`dt`.`id` = `d`.`id`)) AS `tept_code`,(select `t_dept`.`id` from `t_dept` where ((`t_dept`.`del` = 0) and (`t_dept`.`code` = (select substring_index(`dt`.`code`,'-',1) from `t_dept` `dt` where (`dt`.`id` = `d`.`id`))))) AS `unit_id`,(select `t_dept`.`id` from `t_dept` where ((`t_dept`.`del` = 0) and (`t_dept`.`code` = (select substring_index(`dt`.`code`,'-',if(((length(`dt`.`code`) - length(replace(`dt`.`code`,'-',''))) > 0),2,0)) from `t_dept` `dt` where (`dt`.`id` = `d`.`id`))))) AS `department_id`,(select `t_dept`.`id` from `t_dept` where ((`t_dept`.`del` = 0) and (`t_dept`.`code` = (select substring_index(`dt`.`code`,'-',if(((length(`dt`.`code`) - length(replace(`dt`.`code`,'-',''))) = 2),3,0)) from `t_dept` `dt` where (`dt`.`id` = `d`.`id`))))) AS `tept_id`,(select `t_dept`.`name_abbr` from `t_dept` where ((`t_dept`.`del` = 0) and (`t_dept`.`code` = (select substring_index(`dt`.`code`,'-',1) from `t_dept` `dt` where (`dt`.`id` = `d`.`id`))))) AS `unit_name`,(select `t_dept`.`name_abbr` from `t_dept` where ((`t_dept`.`del` = 0) and (`t_dept`.`code` = (select substring_index(`dt`.`code`,'-',if(((length(`dt`.`code`) - length(replace(`dt`.`code`,'-',''))) > 0),2,0)) from `t_dept` `dt` where (`dt`.`id` = `d`.`id`))))) AS `department_name`,(select `t_dept`.`name_abbr` from `t_dept` where ((`t_dept`.`del` = 0) and (`t_dept`.`code` = (select substring_index(`dt`.`code`,'-',if(((length(`dt`.`code`) - length(replace(`dt`.`code`,'-',''))) = 2),3,0)) from `t_dept` `dt` where (`dt`.`id` = `d`.`id`))))) AS `tept_name`,`d`.`full_path` AS `full_path`,`getDeptFullPathName`(`d`.`code`) AS `full_name` from `t_dept` `d` join t_user t on d.id=t.dept_id where t.id= #{userId} and ((`d`.`del` = 0) and ((`d`.`del` = 0) and (`d`.`status` = 1)))) d on u.dept_id=d.dept_id " +
            "where u.id = #{userId}")
    UserInfoVo selectUserInfoByUserId(Long userId);
}
